package com.iretailer.report.web.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReCategories;
import com.iretailer.report.model.Categories;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Bennie
 * @Auther: xjw
 * @Date: 2019/1/25 16:26
 * @Description:
 */
@RestController
public class CategoryController extends BaseAction {

    @RequestMapping("/system/selectCategoryByKeyName.action")
    public Map<String, Object> selectCategoryByKeyName(@RequestBody ReCategories rc) {
        List<Categories> result = categoriesService.findChildrenCategoriesByKeyName(rc.getKeyName());
        return RMap.ok(result);
    }

    @RequestMapping("/system/gridCategory.action")
    public Map<String, Object> gridCategory(@RequestBody ReCategories rc) {
        QueryOption queryOption = modelMapper.map(rc, QueryOption.class);
        PageResult<Categories> result = categoriesService.selectByParentIdPage(rc.getParentId(), queryOption);
        return RMap.ok(result);
    }

    @RequestMapping("/system/CategoryTree.action")
    public Map<String, Object> tree() {
        List<Categories> findCategories = categoriesService.list(new QueryWrapper<Categories>().eq("Visible", 1).orderByAsc("Ranked"));
        Categories first = Categories.FirstCategory;
        List<Categories> tree = categoriesService.convertTree(findCategories, "-");
        first.setChildren(tree);
        return RMap.ok(new Categories[]{first});
    }

    @RequestMapping("/system/selectCategoryById.action")
    public Map<String, Object> selectCategoryById(@RequestBody ReCategories reCategories) {
        if ("-".equals(reCategories.getId())) {
            return RMap.ok(Categories.FirstCategory);
        } else {
            Categories find = categoriesService.getById(reCategories.getId());
            return RMap.ok(find);
        }
    }

    @RequestMapping("/system/saveOrUpdateCategory.action")
    public Map<String, Object> saveOrUpdate(@RequestBody ReCategories rc,HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        Categories categories = modelMapper.map(rc, Categories.class);
        boolean result = categoriesService.saveOrUpdate(categories, user.getName());
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/system/deleteCategory.action")
    public Map<String, Object> del(@RequestBody List<String> ids) {
        boolean result = categoriesService.deleteByIds(ids);
        if (result) {
            return RMap.ok();
        } else {
            return RMap.error(RMap.Code.CONNECTED, "have_children_category");
        }
    }
}
